package GUI;

import ModelLayer.*;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

import javax.swing.border.EtchedBorder;

public class LoginGUI extends JPanel
{
	private JTextField usernameField;
	private JPasswordField passwordField;
	private LoginContainer container;
	private JPanel logScreen;
	private JPanel interPanel;
	private JPanel kundeScreen;
	private JPanel vareScreen;
	private JPanel ordreScreen;
	private CardLayout cardLay;
	
	public LoginGUI()
	{
		container = container.getInstance();
		makeLogScreen();
		setLayout(new BorderLayout(0, 0));
		add(logScreen);
	}
	
	private void makeLogScreen()
	{
		logScreen = new JPanel();
		GridLayout gridLayout = new GridLayout(0,1);
		logScreen.setLayout(gridLayout);
		JPanel panel3 = new JPanel();
		SpringLayout sl_panel3 = new SpringLayout();
		panel3.setLayout(sl_panel3);
		JPanel panel_2 = new JPanel();
		logScreen.add(panel_2);
		
		logScreen.add(panel3);
		JPanel panel_1 = new JPanel();
		logScreen.add(panel_1);
		
		JPanel panel = new JPanel();
		sl_panel3.putConstraint(SpringLayout.NORTH, panel, 10, SpringLayout.NORTH, panel3);
		sl_panel3.putConstraint(SpringLayout.SOUTH, panel, 95, SpringLayout.NORTH, panel3);
		panel3.add(panel);
		sl_panel3.putConstraint(SpringLayout.WEST, panel, 129, SpringLayout.WEST, panel3);
		sl_panel3.putConstraint(SpringLayout.EAST, panel, -90, SpringLayout.EAST, panel3);
		FlowLayout flowLayout = (FlowLayout) panel.getLayout();
		flowLayout.setAlignOnBaseline(true);
		panel.setMaximumSize(new Dimension(150, 150));
		panel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		JPanel panel1 = new JPanel();
		panel.add(panel1);
		panel1.setLayout(new FlowLayout());
		panel1.add(new JLabel("Username:"));
		usernameField = new JTextField(25);
		panel1.add(usernameField);
		JPanel panel2 = new JPanel();
		panel.add(panel2);
		panel2.setLayout(new FlowLayout());
		panel2.add(new JLabel("Password:"));
		passwordField = new JPasswordField(25);
		panel2.add(passwordField);
		
		JButton button = new JButton("Login");
		button.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				login();
			}
		});
		sl_panel3.putConstraint(SpringLayout.SOUTH, button, 30, SpringLayout.SOUTH, panel);
		sl_panel3.putConstraint(SpringLayout.EAST, button, -150, SpringLayout.EAST, panel);
		panel3.add(button);
	}
	
	private void interfacing(boolean admin)
	{
		remove(logScreen);
		JPanel topPanel = new JPanel();
		cardLay = new CardLayout();
		interPanel = new JPanel();
		kundeScreen = new PersonDesign();
		vareScreen = new VareDesign();
		ordreScreen = new OrdreGUI();
		
		setLayout(new BorderLayout());
		topPanel.setLayout(new FlowLayout());
		interPanel.setLayout(cardLay);
		
		/*
		interPanel.add(new PersonDesign());
		interPanel.add(new VareDesign());
		interPanel.add(new OrdreGUI());
		*/
		
		JButton vareB = new JButton("Vare");
		vareB.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				vareMenu();
			}
		});
		topPanel.add(vareB);
		
		JButton kundeB = new JButton("Kunder");
		kundeB.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				kundeMenu();
			}
		});
		topPanel.add(kundeB);
		
		JButton ordreB = new JButton("Ordre");
		ordreB.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				ordreMenu();
			}
		});
		topPanel.add(ordreB);
		
		add(topPanel, BorderLayout.NORTH);
		add(interPanel, BorderLayout.CENTER);
		validate();
	}
	
	private void vareMenu()
	{
		interPanel.remove(vareScreen);
		interPanel.add(vareScreen);
		validate();
	}
	
	private void kundeMenu()
	{
		interPanel.remove(kundeScreen);
		interPanel.add(kundeScreen);
		validate();
	}
	
	private void ordreMenu()
	{
		interPanel.remove(ordreScreen);
		interPanel.add(ordreScreen);
		validate();
	}
	
	private void login()
	{
        String name, pass;
        name = usernameField.getText();
        pass = passwordField.getText();
        switch(container.checkPass(name,pass))
        {
        case 1:
        	interfacing(false);
        	break;
        case 0:
        	logError();
        	break;
        case 2:
        	interfacing(true);
        	break;
        }
	}
	
	private void logError()
	{
		usernameField.setText("Wrong username and/or password");
		passwordField.setText("");
	}
}